//随机参数：10~15名学生，ID按顺序来，年龄随机，成绩随机，性别随机
//
//学生管理系统
//1查看所有学生
//2按年龄排序，从小到大
//3按成绩排序，从大到小


function Student(id,name,sex,age,score){
			this.id=id;
			this.name=name;
			this.sex=sex;
			this.age=age;
			this.score=score;
			this.input=function (){
			return "id="+this.id+",name="+this.name+",sex="+this.sex
			+",age="+this.age+",score="+this.score+"\n";
		}
}

var stuList=new Array();
for(var i=0;i<10;i++){
	var id=i+1;
	var name="stu"+(i+1);
	var sex="";
	if (Math.floor(Math.random()*2)>0) {
		sex='男';

	}else{
		sex='女';
	}
	var age=18+Math.floor(Math.random()*8);
	var score=Math.floor(Math.random()*101);
	var stu=new Student(id,name,sex,age,score);
	stuList.push(stu);

}
function home(){
  var key = prompt('请选择你要进行的操作！\n1.查看所有的学生\n2.按年龄排序\n3.按成绩排序');
          switch(key){
            case '1':
            seeStudent();
            break;
            case '2': 
             ageSort() ;
            break;
            case '3':
            scoreSort();
            break;
            case '4':
            //通过return退出整个程序
            return;
            default:
            alert("亲,输入有误!");
           }
           home();
           }
     // * 查看学生信息
     
     function  seeStudent(){
     	var str="";
     	for(var i=0;i<stuList.length;i++){
     		str +=stuList[i].input();

     	}
     	// 查看信息
     	alert(str);
     }

     function ageSort(){
     		   for (var i = 1; i < stuList.length; i++) {
        	    
        	    for (var j = 0; j < stuList.length-i; j++) {
        	    	 
        	    	 if (stuList[j].age>stuList[j+1].age) {
                            
                            var temp = stuList[j];

                            stuList[j] = stuList[j+1];

                            stuList[j+1] = temp;
        	    	 }
        	    }
        }

        seeStudent();
     }

     function scoreSort(){
   			for (var i = 1; i < stuList.length; i++) {
        	    
        	    for (var j = 0; j < stuList.length-i; j++) {
        	    	 
        	    	 if (stuList[j].score>stuList[j+1].score) {
                            
                            var temp = stuList[j];

                            stuList[j] = stuList[j+1];

                            stuList[j+1] = temp;
        	    	 }
        	    }
        }

        seeStudent();  		

     }
     home();